#!/usr/bin/perl -w
use strict;
use lib ("/net/cpp-group/Leo/bin");
use GO_frequency;
use DBI;
use Getopt::Long;




my $host='db';
my $user='caleb';
my $database= 'ensembl_mart_26_1';
my $password='';
my $dbh_ens = DBI->connect("DBI:mysql:database=$database;host=$host",$user, $password,{'RaiseError' => 1});

#get background

my @bggenes;
open(BGSET,$background) || croak "Error:\n\tCould not open [$background]\n$!";
while (<BGSET>)
{
	chomp;
	push @bggenes,$_;
}

my $numgenes = @bggenes;
print "numgenes $numgenes\n";






#_________________________________________________________________________________________
#
#
#	retrieve_GO_ids_per_category
#
#_________________________________________________________________________________________
sub retrieve_GO_ids_per_category($\%)
{
	my ($dbh, $gene_id_to_GO_ids) = @_;
	my $sql_data = $dbh->selectall_arrayref(<<'PL/SQLCMD');
    SELECT
        GO_category,
		gene_id,
		GO_id, 
	FROM 
		taxon.gene_go_matches
    WHERE
        species = 'homo';
PL/SQLCMD

	# save GO identifiers per GO category per gene id
	for my $line(@$sql_data)
	{
		push(@{$gene_id_to_GO_ids->{$line->[0]}{$line->[1]}}, $line->[2]);
	}

}


#_________________________________________________________________________________________
#
#
#	retrieve_GO_ids_per_category
#
#_________________________________________________________________________________________
sub retrieve_GO_descriptions($\%)
{
	my ($dbh, $GO_descriptions) = @_;
	my $sql_data = $dbh->selectall_arrayref(<<'PL/SQLCMD');
    SELECT
		GO_id, 
        description
	FROM 
		go_term_descriptions;
PL/SQLCMD

	# save GO identifiers per GO category per gene id
	for my $line(@$sql_data)
	{
		$GO_descriptions->{$line->[0]} = $line->[1];
	}

}






#
#	print GO terms which are under or over represented
#
sub print_GO_over_under($$\@\@\@)
{
	my ($dbh, $ens_species, $gene_ids, $gene_ids_background, $significant_results_all) = @_;

	# get GO identifiers and descriptions
	my %gene_id_to_GO_ids_all;
	my %GO_id_to_description;
	retrieve_GO_descriptions($dbh, %GO_id_to_description);
	retrieve_GO_ids_per_category($dbh, %gene_id_to_GO_ids_all);

	
	# get GO slim identifiers
	my %GO_slim_ids;
	get_GO_slims(%GO_slim_ids, "/net/cpp-group/caleb/new_node_stuff/GO/goslim_generic.obo");

	# get significant results
	my %significant_results;
	get_under_overrepresented_GO_terms(	%gene_id_to_GO_ids_all, %GO_slim_ids, 
										 @$gene_ids_background, @$gene_ids, 
										 0.01, %significant_results);


	# print out for each GO category
	for my $GO_cat (keys %significant_results)
	{
		# for each set of under and then over represented
		for my $over_under (keys %{$significant_results->{$GO_cat}})
		{
			# sort GO identifiers by their descriptions
			my @GO_ids = sort {	$GO_id_to_description{$a} cmp
								$GO_id_to_description{$b}}
								keys %{$significant_results->{$GO_cat}{$over_under}};

			# for each GO identifier
			for my $GO_id(@GO_ids)
			{
				printf	"$GO_cat\t$over_under\t$GO_id\t%d\t%d\t%d\t%d\t%1.2e\t%1.2e\n",
							@{$significant_results->{$GO_cat}{$over_under}{$GO_id}};
			}
		}
		
	}
}

